import matplotlib.pyplot as plt
from matplotlib.pylab import mpl
import numpy as np
from numpy import pi, cos, sin

# 中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

x = [8. ,7.28439368 ,6.09309604 ,5.97299605 ,6.92705098 ,7.41421356
 ,6.4845875 ,5.09896401 ,4.80901699 ,5.62188592 ,6. ,4.99614806
 ,3.57294902 ,3.28300201 ,4.13344649 ,4.58578644 ,3.69098301 ,2.40896996
 ,2.28886997 ,3.33364031 ,4. ,3.33364031 ,2.28886997 ,2.40896996
 ,3.69098301 ,4.58578644 ,4.13344649 ,3.28300201 ,3.57294902 ,4.99614806
 ,6. ,5.62188592 ,4.80901699 ,5.09896401 ,6.4845875 ,7.41421356
 ,6.92705098 ,5.97299605 ,6.09309604 ,7.28439368 ,8. ,7.28439368
 ,6.09309604 ,5.97299605 ,6.92705098 ,7.41421356 ,6.4845875 ,5.09896401
 ,4.80901699 ,5.62188592 ,6. ,4.99614806 ,3.57294902 ,3.28300201
 ,4.13344649 ,4.58578644 ,3.69098301 ,2.40896996 ,2.28886997 ,3.33364031
 ,4. ,3.33364031 ,2.28886997 ,2.40896996 ,3.69098301 ,4.58578644
 ,4.13344649 ,3.28300201 ,3.57294902 ,4.99614806 ,6. ,5.62188592
 ,4.80901699 ,5.09896401 ,6.4845875 ,7.41421356 ,6.92705098 ,5.97299605
 ,6.09309604 ,7.28439368 ,8. ,7.28439368 ,6.09309604 ,5.97299605
 ,6.92705098 ,7.41421356 ,6.4845875 ,5.09896401 ,4.80901699 ,5.62188592
 ,6. ,4.99614806 ,3.57294902 ,3.28300201 ,4.13344649 ,4.58578644
 ,3.69098301 ,2.40896996 ,2.28886997 ,3.33364031 ,4. ,3.33364031
 ,2.28886997 ,2.40896996 ,3.69098301 ,4.58578644 ,4.13344649 ,3.28300201
 ,3.57294902 ,4.99614806 ,6. ,5.62188592 ,4.80901699 ,5.09896401
 ,6.4845875 ,7.41421356 ,6.92705098 ,5.97299605 ,6.09309604 ,7.28439368
 ,8. ,7.28439368 ,6.09309604 ,5.97299605 ,6.92705098 ,7.41421356
 ,6.4845875 ,5.09896401 ,4.80901699 ,5.62188592 ,6. ,4.99614806
 ,3.57294902 ,3.28300201 ,4.13344649 ,4.58578644 ,3.69098301 ,2.40896996
 ,2.28886997 ,3.33364031 ,4. ,3.33364031 ,2.28886997 ,2.40896996
 ,3.69098301 ,4.58578644 ,4.13344649 ,3.28300201 ,3.57294902 ,4.99614806
 ,6. ,5.62188592 ,4.80901699 ,5.09896401 ,6.4845875 ,7.41421356
 ,6.92705098 ,5.97299605 ,6.09309604 ,7.28439368 ,8. ,7.28439368
 ,6.09309604 ,5.97299605 ,6.92705098 ,7.41421356 ,6.4845875 ,5.09896401
 ,4.80901699 ,5.62188592 ,6. ,4.99614806 ,3.57294902 ,3.28300201
 ,4.13344649 ,4.58578644 ,3.69098301 ,2.40896996 ,2.28886997 ,3.33364031
 ,4. ,3.33364031 ,2.28886997 ,2.40896996 ,3.69098301 ,4.58578644
 ,4.13344649 ,3.28300201 ,3.57294902 ,4.99614806 ,6. ,5.62188592
 ,4.80901699 ,5.09896401 ,6.4845875 ,7.41421356 ,6.92705098 ,5.97299605
 ,6.09309604 ,7.28439368 ,8. ,7.28439368 ,6.09309604 ,5.97299605
 ,6.92705098 ,7.41421356 ,6.4845875 ,5.09896401 ,4.80901699 ,5.62188592
 ,6. ,4.99614806 ,3.57294902 ,3.28300201 ,4.13344649 ,4.58578644
 ,3.69098301 ,2.40896996 ,2.28886997 ,3.33364031 ,4. ,3.33364031
 ,2.28886997 ,2.40896996 ,3.69098301 ,4.58578644 ,4.13344649 ,3.28300201
 ,3.57294902 ,4.99614806 ,6. ,5.62188592 ,4.80901699 ,5.09896401
 ,6.4845875 ,7.41421356 ,6.92705098 ,5.97299605 ,6.09309604 ,7.28439368
 ,8. ,7.28439368 ,6.09309604 ,5.97299605 ,6.92705098 ,7.41421356
 ,6.4845875 ,5.09896401 ,4.80901699 ,5.62188592 ,6. ,4.99614806
 ,3.57294902 ,3.28300201 ,4.13344649 ,4.58578644 ,3.69098301 ,2.40896996
 ,2.28886997 ,3.33364031 ,4. ,3.33364031 ,2.28886997 ,2.40896996
 ,3.69098301 ,4.58578644 ,4.13344649 ,3.28300201 ,3.57294902 ,4.99614806
 ,6. ,5.62188592 ,4.80901699 ,5.09896401 ,6.4845875 ,7.41421356
 ,6.92705098 ,5.97299605 ,6.09309604 ,7.28439368 ,8. ,7.28439368
 ,6.09309604 ,5.97299605 ,6.92705098 ,7.41421356 ,6.4845875 ,5.09896401
 ,4.80901699 ,5.62188592 ,6. ,4.99614806 ,3.57294902 ,3.28300201
 ,4.13344649 ,4.58578644 ,3.69098301 ,2.40896996 ,2.28886997 ,3.33364031
 ,4. ,3.33364031 ,2.28886997 ,2.40896996 ,3.69098301 ,4.58578644
 ,4.13344649 ,3.28300201 ,3.57294902 ,4.99614806 ,6. ,5.62188592
 ,4.80901699 ,5.09896401 ,6.4845875 ,7.41421356 ,6.92705098 ,5.97299605
 ,6.09309604 ,7.28439368 ,8. ,7.28439368 ,6.09309604 ,5.97299605
 ,6.92705098 ,7.41421356 ,6.4845875 ,5.09896401 ,4.80901699 ,5.62188592
 ,6. ,4.99614806 ,3.57294902 ,3.28300201 ,4.13344649 ,4.58578644
 ,3.69098301 ,2.40896996 ,2.28886997 ,3.33364031 ,4. ,3.33364031
 ,2.28886997 ,2.40896996 ,3.69098301 ,4.58578644 ,4.13344649 ,3.28300201
 ,3.57294902 ,4.99614806 ,6. ,5.62188592 ,4.80901699 ,5.09896401
 ,6.4845875 ,7.41421356 ,6.92705098 ,5.97299605 ,6.09309604 ,7.28439368
 ,8. ,7.28439368 ,6.09309604 ,5.97299605 ,6.92705098 ,7.41421356
 ,6.4845875 ,5.09896401 ,4.80901699 ,5.62188592 ,6. ,4.99614806
 ,3.57294902 ,3.28300201 ,4.13344649 ,4.58578644 ,3.69098301 ,2.40896996
 ,2.28886997 ,3.33364031 ,4. ,3.33364031 ,2.28886997 ,2.40896996
 ,3.69098301 ,4.58578644 ,4.13344649 ,3.28300201 ,3.57294902 ,4.99614806
 ,6. ,5.62188592 ,4.80901699 ,5.09896401 ,6.4845875 ,7.41421356
 ,6.92705098 ,5.97299605 ,6.09309604 ,7.28439368 ,8. ,7.28439368
 ,6.09309604 ,5.97299605 ,6.92705098 ,7.41421356 ,6.4845875 ,5.09896401
 ,4.80901699 ,5.62188592 ,6. ,4.99614806 ,3.57294902 ,3.28300201
 ,4.13344649 ,4.58578644 ,3.69098301 ,2.40896996 ,2.28886997 ,3.33364031
 ,4. ,3.33364031 ,2.28886997 ,2.40896996 ,3.69098301 ,4.58578644
 ,4.13344649 ,3.28300201 ,3.57294902 ,4.99614806 ,6. ,5.62188592
 ,4.80901699 ,5.09896401 ,6.4845875 ,7.41421356 ,6.92705098 ,5.97299605
 ,6.09309604 ,7.28439368 ,8. ,7.28439368 ,6.09309604 ,5.97299605
 ,6.92705098 ,7.41421356 ,6.4845875 ,5.09896401 ,4.80901699 ,5.62188592
 ,6. ,4.99614806 ,3.57294902 ,3.28300201 ,4.13344649 ,4.58578644
 ,3.69098301 ,2.40896996 ,2.28886997 ,3.33364031 ,4. ,3.33364031
 ,2.28886997 ,2.40896996 ,3.69098301 ,4.58578644 ,4.13344649 ,3.28300201
 ,3.57294902 ,4.99614806 ,6. ,5.62188592 ,4.80901699 ,5.09896401
 ,6.4845875 ,7.41421356 ,6.92705098 ,5.97299605 ,6.09309604 ,7.28439368
 ,8. ,7.28439368 ,6.09309604 ,5.97299605 ,6.92705098 ,7.41421356
 ,6.4845875 ,5.09896401 ,4.80901699 ,5.62188592 ,6. ,4.99614806
 ,3.57294902 ,3.28300201 ,4.13344649 ,4.58578644 ,3.69098301 ,2.40896996
 ,2.28886997 ,3.33364031 ,4. ,3.33364031 ,2.28886997 ,2.40896996
 ,3.69098301 ,4.58578644 ,4.13344649 ,3.28300201 ,3.57294902 ,4.99614806
 ,6. ,5.62188592 ,4.80901699 ,5.09896401 ,6.4845875 ,7.41421356
 ,6.92705098 ,5.97299605 ,6.09309604 ,7.28439368 ,8. ,7.28439368
 ,6.09309604 ,5.97299605 ,6.92705098 ,7.41421356 ,6.4845875 ,5.09896401
 ,4.80901699 ,5.62188592 ,6. ,4.99614806 ,3.57294902 ,3.28300201
 ,4.13344649 ,4.58578644 ,3.69098301 ,2.40896996 ,2.28886997 ,3.33364031
 ,4. ,3.33364031 ,2.28886997 ,2.40896996 ,3.69098301 ,4.58578644
 ,4.13344649 ,3.28300201 ,3.57294902 ,4.99614806 ,6. ,5.62188592
 ,4.80901699 ,5.09896401 ,6.4845875 ,7.41421356 ,6.92705098 ,5.97299605
 ,6.09309604 ,7.28439368 ,8. ,7.28439368 ,6.09309604 ,5.97299605
 ,6.92705098 ,7.41421356 ,6.4845875 ,5.09896401 ,4.80901699 ,5.62188592
 ,6. ,4.99614806 ,3.57294902 ,3.28300201 ,4.13344649 ,4.58578644
 ,3.69098301 ,2.40896996 ,2.28886997 ,3.33364031 ,4. ,3.33364031
 ,2.28886997 ,2.40896996 ,3.69098301 ,4.58578644 ,4.13344649 ,3.28300201
 ,3.57294902 ,4.99614806 ,6. ,5.62188592 ,4.80901699 ,5.09896401
 ,6.4845875 ,7.41421356 ,6.92705098 ,5.97299605 ,6.09309604 ,7.28439368
 ,8. ,7.28439368 ,6.09309604 ,5.97299605 ,6.92705098 ,7.41421356
 ,6.4845875 ,5.09896401 ,4.80901699 ,5.62188592 ,6. ,4.99614806
 ,3.57294902 ,3.28300201 ,4.13344649 ,4.58578644 ,3.69098301 ,2.40896996
 ,2.28886997 ,3.33364031 ,4. ,3.33364031 ,2.28886997 ,2.40896996
 ,3.69098301 ,4.58578644 ,4.13344649 ,3.28300201 ,3.57294902 ,4.99614806
 ,6. ,5.62188592 ,4.80901699 ,5.09896401 ,6.4845875 ,7.41421356
 ,6.92705098 ,5.97299605 ,6.09309604 ,7.28439368 ,8. ,7.28439368
 ,6.09309604 ,5.97299605 ,6.92705098 ,7.41421356 ,6.4845875 ,5.09896401
 ,4.80901699 ,5.62188592 ,6. ,4.99614806 ,3.57294902 ,3.28300201
 ,4.13344649 ,4.58578644 ,3.69098301 ,2.40896996 ,2.28886997 ,3.33364031
 ,4. ,3.33364031 ,2.28886997 ,2.40896996 ,3.69098301 ,4.58578644
 ,4.13344649 ,3.28300201 ,3.57294902 ,4.99614806 ,6. ,5.62188592
 ,4.80901699 ,5.09896401 ,6.4845875 ,7.41421356 ,6.92705098 ,5.97299605
 ,6.09309604 ,7.28439368 ,8. ,7.28439368 ,6.09309604 ,5.97299605
 ,6.92705098 ,7.41421356 ,6.4845875 ,5.09896401 ,4.80901699 ,5.62188592
 ,6. ,4.99614806 ,3.57294902 ,3.28300201 ,4.13344649 ,4.58578644
 ,3.69098301 ,2.40896996 ,2.28886997 ,3.33364031 ,4. ,3.33364031
 ,2.28886997 ,2.40896996 ,3.69098301 ,4.58578644 ,4.13344649 ,3.28300201
 ,3.57294902 ,4.99614806 ,6. ,5.62188592 ,4.80901699 ,5.09896401
 ,6.4845875 ,7.41421356 ,6.92705098 ,5.97299605 ,6.09309604 ,7.28439368
 ,8. ,7.28439368 ,6.09309604 ,5.97299605 ,6.92705098 ,7.41421356
 ,6.4845875 ,5.09896401 ,4.80901699 ,5.62188592 ,6. ,4.99614806
 ,3.57294902 ,3.28300201 ,4.13344649 ,4.58578644 ,3.69098301 ,2.40896996
 ,2.28886997 ,3.33364031 ,4. ,3.33364031 ,2.28886997 ,2.40896996
 ,3.69098301 ,4.58578644 ,4.13344649 ,3.28300201 ,3.57294902 ,4.99614806
 ,6. ,5.62188592 ,4.80901699 ,5.09896401 ,6.4845875 ,7.41421356
 ,6.92705098 ,5.97299605 ,6.09309604 ,7.28439368 ,8. ,7.28439368
 ,6.09309604 ,5.97299605 ,6.92705098 ,7.41421356 ,6.4845875 ,5.09896401
 ,4.80901699 ,5.62188592 ,6. ,4.99614806 ,3.57294902 ,3.28300201
 ,4.13344649 ,4.58578644 ,3.69098301 ,2.40896996 ,2.28886997 ,3.33364031
 ,4. ,3.33364031 ,2.28886997 ,2.40896996 ,3.69098301 ,4.58578644
 ,4.13344649 ,3.28300201 ,3.57294902 ,4.99614806 ,6. ,5.62188592
 ,4.80901699 ,5.09896401 ,6.4845875 ,7.41421356 ,6.92705098 ,5.97299605
 ,6.09309604 ,7.28439368]

N = len(x)
freq = np.arange(N)  # 频率counter
fs = N
t = np.arange(0, 1, 1.0 / fs)  # 1秒采样fs个点

X1 = np.fft.fft(x)  # 离散傅里叶变换

# 自编计算
X2 = np.zeros(N, dtype=np.complex)  # X[n]
for k in range(0, N):  # 0,1,2,...,N-1
    for n in range(0, N):  # 0,1,2,...,N-1
        # X[k] = X[k] + x[n] * np.exp(-2j * pi * k * n / N)
        X2[k] = X2[k] + (1 / N) * x[n] * np.exp(-2j * pi * k * n / N)


# 散点图
plt.subplot(4, 1, 1)
plt.plot(t, x, 'go')
plt.title('散点图')
plt.xlabel('Time(s)')
plt.ylabel('Amplitude')

# 曲线图
plt.subplot(4, 1, 2)
plt.plot(t, x)
plt.title('曲线图')
# plt.xlabel('Time(s)')
plt.subplot(4, 1, 3).set_title('Time(s)')
plt.ylabel('Amplitude')

freq_half = freq[range(int(N / 2))]  # 前一半频率
X_half = X2[range(int(N / 2))]

# 频谱图-用函数
plt.subplot(4, 1, 3)
plt.plot(freq_half, X_half)
plt.title('频谱图-用函数')
plt.xlabel('Frequence')
plt.ylabel('Amplitude')

# 频谱图-自编计算
plt.subplot(4, 1, 4)
plt.plot(freq_half, X_half)
plt.title('频谱图-自编计算')
plt.xlabel('Frequence')
plt.ylabel('Amplitude')

plt.tight_layout()   # 自动调整子图间距


plt.show()